package student;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2022-12-14
 * Time: 7:13
 */
public class StudentList {
    public ArrayList<Student> students = new ArrayList<>(50);
    public int usedsize;//存放当前学生个数

    /**
     * 事先通过构造方法,初始化的时候,给ArrayList里面预存3个学生,usedSize=3;
     */
    public StudentList(){
        students.add(new Student(1,"白糖","软件",19,"M"));
        students.add(new Student(2,"大飞","软件",20,"M"));
        students.add(new Student(3,"小青","软件",19,"F"));
        this.usedsize = students.size();
    }
    public int getUsedsize(){
        return usedsize;
    }
    public void setUsedsize(int usedSize){
        this.usedsize = usedSize;
    }
    public Student getPos(int pos){
        return students.get(pos);
    }
    public void setStudents(Student student,int pos){
        students.set(pos,student) ;
    }
    public  void addStudents(Student student){
        students.add(student);
    }
    public void setStudents(ArrayList<Student> students){
        this.students = students;
    }
    public void display(){
        Iterator<Student> it = students.iterator();
        while (it.hasNext()){
             Student student = it.next();
            System.out.println(student);
        }
    }
    public void remveStudent(int pos){
        students.remove(pos);
    }
    public void Myclear(){
        students.clear();
    }
    public boolean isEmpty(){
        return students.isEmpty();
    }
    public int findIDpos(int id){
        int index = -1;
        for (int i = 0; i < usedsize; i++) {
           Student student = getPos(i);
           if(id == student.getId()) {
               index = i;
           }
        }
        return index;
    }
    public int findNamepos(String name){
        int index = -1;
        for (int i = 0; i < usedsize; i++) {
            Student student = getPos(i);
            if(name.equals(student.getName()) ) {
                index = i;
            }
        }
        return index;
    }
    public boolean isIDReapt(String id) {
        if(isEmpty()){
            return false;
        }
        int newid = Integer.valueOf(id);
        int index = findIDpos(newid);
        if (index == -1) {
            return false;
        } else {
            return true;
        }

    }
    public void SortCompare(Comparator s){
        Collections.sort(students,s);
        System.out.println(students);
    }
}
